<?php
/**
 * @package    panel
 * @subpackage panel.environment
 */
/**
 * ENV shortuct/front controller class.
 * @author Dmitry Bakaleinik (code)
 */
class panel__environment
	implements maf__i__singletone, maf__mvc__i__model {

	protected function __clone() {
	}	# Function

	protected function __wakeup() {
	}	# Function

	protected function __sleep() {
	}	# Function

	/**
	 * @return panel__environment
	 */
	static public function getInstance() {

		static $instance;

		if( !isset($instance) ) {
			$instance = new self; }

		return $instance;

	}	# Function

	/**
	 * Init code/execution environment
	 *
	 *\msc
	 *Environment;
	 *Environment box Environment [label="1. Display errors"];
	 *Environment box Environment [label="2. Final exception handler"];
	 *Environment box Environment [label="3. Zlib compression"];
	 *Environment box Environment [label="4. Timezone"];
	 *Environment box Environment [label="5. Test of logs/cache directories"];
	 *Environment box Environment [label="6. DB connection testing.", URL="\ref panel__db::testConnection()"];
	 *Environment box Environment [label="7. Doctrine initialization."];
	 *\endmsc
	 *
	 * @return bool
	 */
	public function init() {

		/**
		 * Init Environment
		 */
		ini_set("display_errors", "on");

		if(
			panel__settings__environment::getState() ==
			panel__settings__environment::STATE__LOCAL
		) {

			maf()->registerE2E(E_ALL | E_STRICT);

		}	# if

		set_exception_handler( array(
			"panel__application",
			"catchException"
		) );

		date_default_timezone_set("Asia/Jerusalem");

		ini_set("zlib.output_compression_level", 6);

		if( panel__settings__environment::isUseGZIPOutput() ) {

			ob_start("ob_gzhandler", 6);

		} else {

			ob_start();

		}	# if

		ob_implicit_flush(FALSE);

		/**
		 * Test Environment
		 */
		switch(FALSE) {

		case is_writeable(PANEL__PATH__LOGS):

			throw new panel__e__exception
				("Logs directory is not writeable. Please check permissions.", E_USER_ERROR);

			break;

		case is_writable(PANEL__PATH__CACHE):

			throw new panel__e__exception
				("Cache directory is not writeable. Please check permissions.",	E_USER_ERROR);

			break;

		}	# switch

		/**
		 * DB Connection
		 */

		p()->db()->testConnection();

		return TRUE;

	}	# Function

	/**
	 * @return panel__environment__catalog
	 */
	public function getCatalog() {

		return panel__environment__catalog::getInstance();

	}	# Function

	/**
	 * Get environment data
	 * @return array
	 */
	public function getEnvironment() {

		return array(

			"combined_id" => panel__settings__environment::getCombinedID(),
			"hostname"    => isset_or($_SERVER['HTTP_HOST']),
			"status"      => $this->getStatus(),
			"debug"       => panel__settings__environment::isDebug(),
			"state"       => panel__settings__environment::getState(),
			"version"     => panel__settings__environment::getVersion(),

		);

	}	# Function

	/**
	 * Get CP status data
	 * @return array
	 */
	public function getStatus() {

		$status = array(
		);

		return $status;

	}	# Function

}	# Class

/* EOF */